package pers.huangyuhui.sms.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import pers.huangyuhui.sms.model.AdminInfo;
import pers.huangyuhui.sms.model.StudentInfo;
import pers.huangyuhui.sms.model.TeacherInfo;
import pers.huangyuhui.sms.service.AdminService;
import pers.huangyuhui.sms.service.StudentService;
import pers.huangyuhui.sms.service.TeacherService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("personalController")
public class PersonalController {

    // 获取 Service 层的对象
    @Resource(name = "adminService")
    private AdminService adminService;
    @Resource(name = "teacherService")
    private TeacherService teacherService;
    @Resource(name = "studentService")
    private StudentService studentService;

    @PostMapping("/editPassword.do")
    private void editPassword(HttpSession session,
                              HttpServletResponse response,
                              @RequestParam(value = "oldPassword") String oldPassword,
                              @RequestParam(value = "newPassword") String newPassword) throws IOException {

        // 获取用户信息
        int userType = (int) session.getAttribute("userType");

        // 提前声明一个 map 对象,Service层需要
        Map<String , Object> map = new HashMap<>();

        switch (userType) {
            case 1:
                // 从 session 中获取已登录的用户信息
                AdminInfo adminInfo = (AdminInfo) session.getAttribute("userInfo");
                if (oldPassword.equals(adminInfo.getPassword())) {
                    if (adminService.modifyPassword(newPassword , adminInfo.getId())) {
                        response.getWriter().write("success");
                    } else {
                        response.getWriter()
                                .write("error: sorry ~ system error ! please check the database connection or try again !");
                    }
                } else {
                    response.getWriter().write("原密码错误哟 !");
                }
                break;
            case 2:
                StudentInfo studentInfo = (StudentInfo) session.getAttribute("userInfo");
                if (oldPassword.equals(studentInfo.getPassword())) {
                    map.put("id" , studentInfo.getId());
                    map.put("password" , newPassword);
                    if (studentService.modifyPassword(map)) {
                        response.getWriter().write("success");
                    } else {
                        response.getWriter()
                                .write("error: sorry ~ system error ! please check the database connection or try again !");
                    }
                } else {
                    response.getWriter().write("原密码错误哟 !");
                }
                break;
            case 3:
                TeacherInfo teacherInfo = (TeacherInfo) session.getAttribute("userInfo");
                if (oldPassword.equals(teacherInfo.getPassword())) {
                    map.put("id" , teacherInfo.getId());
                    map.put("password" , newPassword);
                    if (teacherService.modifyPassword(map)) {
                        response.getWriter().write("success");
                    } else {
                        response.getWriter().write("error: sorry ~ system error ! please check the database connection or try again !");
                    }
                } else {
                    response.getWriter().write("原密码错误哟 !");
                }
                break;
        }
    }
}
